<?php
class Profile_Controller {
	public function CheckLength($input, $type) {
		$length = strlen($input);
		if ($length == 0) {
			echo "<script language='javascript'>";
			echo "alert('".$type."不得為空');";
			echo "history.back();";
			echo "</script>";
			die();
		}
	}
	
	public function showProfile($id) {
		require('../host_config.php');
		require_once('../Model/MySQL.php');
		$mysql = new MySQL();
		$link = $mysql->connect($mysql_host, $mysql_user, $mysql_password, $mysql_db);
		$sql = "SELECT account, name, birth, tel, pic  FROM user WHERE uid = $id "; 
		$row = $mysql->query_row($sql, $link);
		return $row;	
	}
	
	public function profileEdit($id, $name, $birth, $tel) {
		require('../host_config.php');
		require_once('../Model/MySQL.php');
		$mysql = new MySQL();
		$link = $mysql->connect($mysql_host, $mysql_user, $mysql_password, $mysql_db);
		Profile_Controller::CheckLength($name, "姓名");
		Profile_Controller::CheckLength($birth, "生日");
		Profile_Controller::CheckLength($tel, "電話");
		$sql = "UPDATE user SET name = '$name', birth = '$birth', tel = '$tel' WHERE uid = '$id' ";
		$mysql->query($sql, $link);
	}
	public function profileEditPic($id, $new_file) {
		require('../host_config.php');
		require_once('../Model/MySQL.php');
		$mysql = new MySQL();
		$link = $mysql->connect($mysql_host, $mysql_user, $mysql_password, $mysql_db);
		$new_file = Profile_Controller::Upload_Pic($new_file);
		$sql = "UPDATE user SET pic = '$new_file' WHERE uid = '$id' ";
		$mysql->query($sql, $link);
	}
	public function Upload_Pic($new_file) {
		$upload_path = "pics/";
		$possible = "_0123456789"."abcdefghijklmnopqrstuvwxyz"."ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$temp = 0;
		/*temp 用於計算檔案上傳的錯誤
		 店面照片已有進行驗證
		如果照片未上傳擇$temp++
		如果附檔名有誤$temp = 10
		如果上傳檔案大小有誤$temp = 20
		如上傳失敗$temp = 30
		*/
		$max_size = 1*1024*1024; //限制可檔案大小為1MB
		$limitedext = array("bmp","gif","jpg","jpeg","png");//設定可上傳的檔案類型(副檔名)
		$str = "";
		while(strlen($str) < 3) {
			$str .= substr($possible, (rand() % strlen($possible)), 1);
		}
		$uploadfilename=time().$str;
		$file_name = $new_file['name'];
		$file_tmp = $new_file['tmp_name'];
		$File_Extension = explode(".", $new_file['name']);
		$File_Extension = $File_Extension[count($File_Extension)-1];
		if($file_name == NULL){
			$file = "pics/default.jpg";
		}
		else if($new_file['error'] > 0){
			echo "照片上傳錯誤代碼:".$_new_file['error'];
			exit;
		}
		else if(($max_size > 0) && ($new_file['size'] > $max_size)){
			echo "照片上傳的檔案大小大於".$max_size."位元組";
			$temp = 20;
		}
		else if(!in_array($File_Extension,$limitedext)){
			echo "$File_Extension <br /> $limitedext <br />";
			echo "照片不支援此檔案類型<br />";
			$temp = 10;
		}
		if(move_uploaded_file($file_tmp, "../".$upload_path.$uploadfilename.".".$File_Extension)){
		$file = $upload_path.$uploadfilename.".".$File_Extension;
		}
		else{
		$temp = 30;
		}
		//Exception
		if ($temp == 10) {
			echo "<script language='javascript'>";
			echo "alert('照片副檔名有誤（只允許GIF和JPEG檔）');";
			echo "history.back();";
			echo "</script>";
			die();
			}else if ($temp == 20) {
			echo "<script language='javascript'>";
			echo "alert('無法上傳照片，請檢查檔案是否小於400 KB');";
			echo "history.back();";
			echo "</script>";
			die();
			}else if ($temp == 30) {
			echo "<script language='javascript'>";
			echo "alert('照片上傳錯誤');";
			echo "history.back();";
			echo "</script>";
				die();
			}
			if ($file[4] == NULL) {
				echo "<script language='javascript'>";
				echo "alert('店家招牌沒有上傳');";
				echo "history.back();";
				echo "</script>";
				die();
			}
			return $file;
		}
	
	}
?>